package handler

import (
	"fmt"
	"fresh-e-commerce/dao"
	"fresh-e-commerce/pkg"
	"fresh-e-commerce/request"
	"fresh-e-commerce/response"
	"github.com/gin-gonic/gin"
)

func UserSms(c *gin.Context) {
	var data request.UserSmsRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	err := dao.UserSms(data.Mobile, data.Source)
	if err != nil {
		response.ResponseError(c, "验证码发送失败", err.Error())
		return
	}
	response.ResponseSuccess(c, "验证码发送成功", nil)
}
func UserLogin(c *gin.Context) {
	var data request.UserLoginRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	userId, err := dao.UserLogin(data.Mobile, data.Sms)
	if err != nil {
		response.ResponseError(c, "登录失败", err.Error())
		return
	}
	claims := pkg.CustomClaims{ID: uint(userId)}
	token, err := pkg.NewJWT("qq").CreateToken(claims)
	if err != nil {
		response.ResponseError(c, "token加密失败", err.Error())
		return
	}
	response.ResponseSuccess(c, "登录成功", token)
}
func OrderCreate(c *gin.Context) {
	userId := c.GetUint("userId")
	fmt.Println(userId)
	var data request.OrderCreateRequest
	if err := c.ShouldBind(&data); err != nil {
		response.ResponseError(c, "错误", err)
		return
	}
	create, err := dao.OrderCreate(int64(userId), data.ProductId, data.Num)
	if err != nil {
		response.ResponseError(c, "订单创建失败", err.Error())
		return
	}
	response.ResponseSuccess(c, "订单创建成功", create)
}
func OrderList(c *gin.Context) {
	userId := c.GetUint("userId")
	list, err := dao.OrderList(int64(userId))
	if err != nil {
		response.ResponseError(c, "", err.Error())
		return
	}
	response.ResponseSuccess(c, "订单信息查询成功", list)
}
